package org.mayuanxiaonong.timserver.handler.deliver;

import org.mayuanxiaonong.timserver.channel.ChannelManager;
import org.mayuanxiaonong.timserver.packet.MessagePacket;
import org.mayuanxiaonong.timserver.packet.Packet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 消息分发器
 * 
 * @author 9527
 *
 */
public class MessageDeliver extends Deliver<Integer> {
	
	private static final Logger log = LoggerFactory.getLogger(MessageDeliver.class);

	public MessageDeliver() {
		this(3);
	}

	public MessageDeliver(int times) {
		super(DeliverQueue.MESSAGE_QUEUE, times);
	}

	@Override
	public Integer handle0(Packet obj) {
		MessagePacket packet = (MessagePacket) obj;
		ChannelManager.getChannel(packet.getTo()).writeAndFlush(packet);
		return 0;
	}

	@Override
	public boolean isSuccess() {
		if (result == 0) { // 根据具体情况写0或者其他值，与deliver方法中返回的值一致
			return true;
		}
		log.error("completion result = " + result);
		return false;
	}

	@Override
	public void onSuccess() {

	}

	@Override
	public void onFailure() {

	}

}
